package XZK010205.dao;

import XZK010205.bean.Express;

/**
 * 快递E站-存储快递信息的操作类
 *
 * @author bing.z 2021/5/31 20:40
 **/
public class ExpressDao {
    /**
     * 二维数组模拟快递柜
     */
    private Express[][] expressesList;
    private int size = 0;

    {
        /**
         * 初始化快递柜
         */
        expressesList = new Express[10][10];
    }

    /**
     * 获取所有快递信息
     *
     * @return
     */
    public Express[][] getAll() {
        return expressesList;
    }

    /**
     * 新增快递信息
     *
     * @param x
     * @param y
     * @param express
     * @return
     */
    public boolean insert(int x, int y, Express express) {
        if (x < 0 || y < 0 || x > 10 || y > 10) {
            return false;
        }
        expressesList[x][y] = express;
        return true;
    }

    public int getSize() {
        return size;
    }

    /**
     * 更加快递单号删除快递
     *
     * @param number
     * @return
     */
    public boolean delete(String number) {
        if (expressesList == null || expressesList.length == 0) {
            return false;
        }
        for (int i = 0; i < expressesList.length; i++) {
            for (int j = 0; j < expressesList[i].length; j++) {
                if (expressesList[i][j] != null && expressesList[i][j].getNumber().equals(number)) {
                    // 删除快递信息
                    expressesList[i][j] = null;
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 根据传入对象的快递单号，更新快递信息
     *
     * @param express
     * @return
     */
    public boolean update(Express express) {
        if (expressesList == null || expressesList.length == 0) {
            return false;
        }
        for (int i = 0; i < expressesList.length; i++) {
            for (int j = 0; j < expressesList[i].length; j++) {
                if (expressesList[i][j] != null && expressesList[i][j].getNumber().equals(express.getNumber())) {
                    // 更新快递信息
                    expressesList[i][j] = express;
                    return true;
                }
            }
        }
        return false;
    }

    /**
     * 根据取件码获取对象
     *
     * @param code
     * @return
     */
    public Express getByCode(int code) {
        if (expressesList == null || expressesList.length == 0) {
            return null;
        }
        for (Express[] rows : expressesList) {
            for (Express express : rows) {
                if (express != null && express.getCode() == code) {
                    return express;
                }
            }
        }
        return null;
    }

    /**
     * 根据单号获取快递对象
     *
     * @param number
     * @return
     */
    public Express getByNumber(String number) {
        if (expressesList == null || expressesList.length == 0) {
            return null;
        }
        for (Express[] rows : expressesList) {
            for (Express express : rows) {
                if (express != null && express.getNumber().equals(number)) {
                    return express;
                }
            }
        }
        return null;
    }
}
